package com.maxuxu.scihome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxuxu.scihome.mapper.DepMapper;
import com.maxuxu.scihome.pojo.Department;
import com.maxuxu.scihome.pojo.Pages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: DepartmentService
 * @author: Maxuxu
 * @date: 2022/7/4 21:45
 * @description: department的服务类
 */
@Service
@Slf4j
public class DepartmentService {
    @Autowired
    DepMapper depMapper;
    @Autowired
    Pages pages;
    /**
     * 添加部门
     * @param department
     */
    public void insertDepartment(Department department){
    depMapper.insert(department);
}

    public Pages selectPage(long current, long size){
        Page<Department> Page = depMapper.selectPage(new Page<>(current, size), null);
        List<Department> records = Page.getRecords();
        long total = Page.getTotal();
        long pages = Page.getPages();
        long current1 = Page.getCurrent();
        long size1 = Page.getSize();
        this.pages.setPages(pages);
        this.pages.setCurrent(current1);
        this.pages.setSize(size1);
        this.pages.setTotal(total);
        this.pages.setRecords(records);
        return this.pages;
    }


    public Department selectByAny(Department department){
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("name", department.getName());
        Department department1 = depMapper.selectOne(wrapper);
        return department1;
    }

    public void deletdepartment(List<Integer> list){
        depMapper.deleteBatchIds(list);
    }

    public void updateDepartmet(String oldname,Department department){
        UpdateWrapper<Department> wrapper = new UpdateWrapper<>();
        wrapper.eq("name", oldname);
        depMapper.update(department, wrapper);

    }
}
